//
//  PureAlert.swift
//  SwiftuiZsa
//
//  Created by hccc on 2023/7/21.
//

import SwiftUI

struct PureAlert<T: View>: ViewModifier {
    // MARK: - ----------------------------------info
    var enableTapDimming: Bool
    var contentView: T
    var dimmingColor: Color
    var transition: AnyTransition
    @Binding var isShow: Bool
    // MARK: - ----------------------------------system
    init(
        isShow: Binding<Bool>,
        enableTapDimming: Bool = true,
        dimmingColor: Color = .black.opacity(0.3),
        transition: AnyTransition,
        @ViewBuilder contentView: () -> T
    ) {
        self._isShow = isShow
        self.enableTapDimming = enableTapDimming
        self.dimmingColor = dimmingColor
        self.transition = transition
        self.contentView = contentView()
    }
    // MARK: - ----------------------------------ui
    func body(content: Content) -> some View {
        ZStack {
            content.zIndex(0)
            
            if isShow {
                dimmingColor
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .ignoresSafeArea()
                    .ifIs(enableTapDimming, transform: { v in
                        v.onTapGesture {
                            withAnimation {
                                isShow = false
                            }
                        }
                    })
                    .transition(.opacity)
                    .animation(.linear(duration: 2), value: isShow)
                    .zIndex(1)
                
                contentView
                    .transition(transition)
                    .zIndex(2)
            }
        }
    }
}

struct PureAlert_Previews: PreviewProvider {
    static var previews: some View {
        Text("")
    }
}
